﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
    public partial class frm_ToolAdd : DevExpress.XtraEditors.XtraForm
    {
        private ucTool parent;

        public frm_ToolAdd(ucTool fTool)
        {
            InitializeComponent();
            parent = fTool;
            this.CancelButton = this.btCancel;
            this.ActiveControl = txtToolID;

        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            if (GetData(helper.ValueObject))
            {
                Utilities.Timer timer = new Utilities.Timer();

                if (helper.Insert() > 0)
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.AddSuccess);

                    rdTool.Checked = true;
                    txtToolID.Text = "";
                    txtToolName.Text = "";
                    txtMakerToolname.Text = "";
                    txtPrice.Text = "";
                    txtSafeQty.Text = "";
                    cmbFrequency.SelectedItem = cmbFrequency.Items[0];
                    cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];
                    txtToolID.Focus();
                }
                else
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_ToolAdd_Load(object sender, EventArgs e)
        {
            BindDataToMaker();
            BindDataToFrequency();
            BindDataToToolGroup();
            rdTool.Checked = true;

        }

        private void rdTool_CheckedChanged(object sender, EventArgs e)
        {
            cmbMakerID.SelectedItem = cmbMakerID.Items[0];
            cmbMakerID.Enabled = false;
        }

        private void rdQTool_CheckedChanged(object sender, EventArgs e)
        {
            cmbMakerID.Enabled = true;
        }

        #region Private methods

        private bool CheckDuplicate(string ToolId)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            helper.ValueObject.ToolID = ToolId;
            DataTable dt = helper.GetOne();
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }

        private bool GetData(Tools o)
        {
            #region Check valid
            if (rdQTool.Checked && Utilities.IsNullOrEmpty(cmbMakerID.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.MakerCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (Utilities.IsNullOrEmpty(txtToolID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtToolName.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtMakerToolname.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.MakerToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbFrequency.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.FrequencyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolGroup.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolGroupCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (decimal.Parse(txtPrice.Text.Trim()) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Tool.PriceMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtSafeQty.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.SafeQtyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (decimal.Parse(txtSafeQty.Text.Trim()) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Tool.SafeQtyMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (CheckDuplicate(txtToolID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.ToolID = txtToolID.Text;
            o.ToolName = txtToolName.Text;
            o.MakerToolName = txtMakerToolname.Text;
            o.Price = decimal.Parse(txtPrice.Text);
            o.SafeQty = int.Parse(txtSafeQty.Text);
            o.FrequencyID = cmbFrequency.SelectedValue.ToString();
            o.ToolGroupID = cmbToolGroup.SelectedValue.ToString();
            o.MakerID = cmbMakerID.SelectedValue.ToString();
            if (rdTool.Checked)
            {
                o.ToolOrQTool = ToolConstants.Configuration.ToolType.IsTool;
            }
            else
            {
                o.ToolOrQTool = ToolConstants.Configuration.ToolType.IsQTool;

            }

            return true;
        }

        private void BindDataToFrequency()
        {
            FrequencyHelper helper = new FrequencyHelper(new Frequency());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbFrequency.DataSource = dt;
            cmbFrequency.DisplayMember = ToolConstants.Entities.Frequency.FieldName.FullName;
            cmbFrequency.ValueMember = ToolConstants.Entities.Frequency.FieldName.FrequencyID;
            row[ToolConstants.Entities.Frequency.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Frequency.FieldName.FrequencyID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbFrequency.SelectedItem = cmbFrequency.Items[0];

        }

        private void BindDataToToolGroup()
        {
            ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbToolGroup.DataSource = dt;
            cmbToolGroup.DisplayMember = ToolConstants.Entities.ToolGroup.FieldName.FullName;
            cmbToolGroup.ValueMember = ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID;
            row[ToolConstants.Entities.ToolGroup.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];

        }

        private void BindDataToMaker()
        {
            MakerHelper helper = new MakerHelper(new Maker());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbMakerID.DataSource = dt;
            cmbMakerID.DisplayMember = ToolConstants.Entities.Maker.FieldName.FullName;
            cmbMakerID.ValueMember = ToolConstants.Entities.Maker.FieldName.MakerID;
            row[ToolConstants.Entities.Maker.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Maker.FieldName.MakerID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMakerID.SelectedItem = cmbMakerID.Items[0];

        }

        #endregion


    }
}